// pages/cart/cart.js
import toast from "../../miniprogram_npm/@vant/weapp/toast/toast"
import { changecarts, changeCheck, getCratList, removeCrats } from "../../server/carts"
import { getUser } from "../../server/user"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cratList: [],
    checked: false,
    totalPrice: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getUser()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCrat()
  },
  changeAllCheck() {
    let goods = this.data.cratList
    if (goods.length > 0) {
      let x = 1
      goods.forEach(item => {
        if (item.is_checked) {
          return
        } else {
          return x = 0
        }
      })
      if (x) {
        this.setData({
          checked: true
        })
      } else {
        this.setData({
          checked: false
        })
      }
    }else{
      this.setData({
        checked: false
      })
    }
  },

  getCrat() {
    const data = {
      include: "goods"
    }
    getCratList(data).then(res => {
      // console.log(res);
      this.setData({
        cratList: res.data
      })
      this.getNumprice()
      this.changeAllCheck()
      // console.log(this.data.cratList);
    })
  },
  changeChecked(e) {
    // console.log(e.target.dataset.id);
    // console.log(e.target.dataset.check);
    let goods = this.data.cratList
    if (e.target.dataset.check) {
      goods = goods.filter(
        item => {
          if (item.id != e.target.dataset.id) {
            if (item.is_checked) {
              return item
            }
          }
        }
      )
      // console.log("取消", goods);
    } else {
      goods = goods.filter(
        item => {
          if (item.id == e.target.dataset.id) {
            return item
          } else {
            if (item.is_checked) {
              return item
            }
          }
        }
      )
      // console.log("添加", goods);
    }
    let data = []
    goods.forEach(item => {
      data = [...data, item.id]
    });
    // console.log(data);
    changeCheck({ cart_ids: data }).then(res => {
      // console.log(res);
      this.getCrat()
      this.changeAllCheck()
    })

  },
  onChange(e) {
    // console.log(e);
    if (this.data.checked) {
      changeCheck({ cart_ids: [] }).then(res => {
        // console.log(res);
        this.getCrat()
      })
    } else {
      let data = []
      let goodsList = this.data.cratList
      goodsList.forEach(item => {
        data = [...data, item.id]
      })
      changeCheck({ cart_ids: data }).then(res => {
        // console.log(res);
        this.getCrat()
      })
    }
    this.setData({
      checked: !this.data.checked
    })
  },
  numOnChange(e) {
    // console.log(e.detail);
    // console.log(e.target.dataset.id);
    // let goodsList = this.data.cratList
    // let goods = goodsList.filter( item => {
    //   if(item.id == e.target.dataset.id){
    //     item.num = e.detail
    //     return item
    //   }
    // })
    // console.log(goods);
    let data = {
      num: e.detail
    }
    changecarts(e.target.dataset.id, data)
      .then(res => {
        // console.log(res);
        this.getCrat()
      })
  },
  getNumprice() {
    let goodsList = this.data.cratList
    let numPrice = goodsList.reduce((pre,curr) => {
      if(curr.is_checked){
        return pre + curr.num * curr.goods.price
      }else{
        return pre
      }
    },0)
    // console.log(numPrice);
    // goodsList.forEach(item => {
    //   if (item.is_checked) {
    //     numPrice += item.num * item.goods.price
    //   }
    // })
    
    // goodsList.filter(item => {
    //   if(item.is_checked){
    //     numPrice += item.num * item.goods.price
    //   }
    // })
    // console.log(numPrice);
    this.setData({
      totalPrice: numPrice
    })
  },
  deleteCrat(e) {
    // console.log(e.target.dataset.id);
    removeCrats(e.target.dataset.id).then(() => {
      toast.success("删除成功")
      // this.getCrat()
      let goodsList = this.data.cratList.filter(item => item.id != e.target.dataset.id)
      // console.log(goodsList);
      // this.setData({
      //   cratList:goodsList
      // })
      // getNumprice()
      // console.log(this.data.cratList);
      this.setData({
        cratList:goodsList
      })
      this.getNumprice()
      this.changeAllCheck()
    })
  },
  toPreview(){
    // let goodsList = this.data.cratList
    // goodsList = goodsList.indexOf()
    if(this.data.totalPrice > 0){
      wx.navigateTo({
        url: '/pages/preview/preview',
      })
    }else{
      wx.showToast({
        title: '请先选择',
        icon:"none"
      })
    }
  }
})